write.tag <- function(tagfile,tag.obj) {
  # by Simon Hoyle June 2008
  # - output comment labels for Tag program

  x <- tag.obj
  a <- "# RELEASE GROUPS    STARTING LENGTH    NUMBER INTERVALS    INTERVAL LENGTH"
  a <- rbind(a,paste("       ",x$hd,collapse="       "),"#","#")
  a <- rbind(a,"# TAG RECOVERIES", paste(c("# ",1:x$hd$nrel),collapse="   "))
  x$nrecov.grp <- table(factor(x$rel.recov$grp,levels=1:x$hd$nrel))
  a <- rbind(a,paste(c("  ",x$nrecov.grp),collapse="   "))
  for (i in 1:x$hd$nrel)
  {

    a <- rbind(a,"#","#","#---------------------------------")
    a <- rbind(a,paste(c("#   ",i," - RELEASE REGION    YEAR    MONTH   Tag_program ",x$tagprog[i]),collapse=""))
    a <- rbind(a,paste(c("    ",x$rel[i,]),collapse="         "))
    a <- rbind(a,paste(x$rel.lens[i,],collapse=" "))
    if (x$nrecov.grp[i] > 0)
    {
      a <- rbind(a,"#","#","# LENGTH RELEASE    FISHERY    RECAP YEAR    RECAP MONTH    NUMBER")
      tags <- x$rel.recov[x$rel.recov[,1]==i,2:6]
      for (j in 1:dim(tags)[1])
      {
        a <- rbind(a,paste(c("",tags[j,]),collapse="          "))
      }
    }
  }
  writeLines(a,tagfile)
}

